//
//  StaffLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "CommunityInfoLogic.h"
#import "OperationDataBaseModel.h"

@implementation CommunityInfoLogic

-(void)insertCommunityData:(CommunityInfoModel *)model withCommuntiyId:(NSString *)communityId member_timestamp:(NSString *)member_timestamp customer_timestamp:(NSString *)customer_timestamp group_timestamp:(NSString *)group_timestamp room_timestamp:(NSString *)room_timestamp
{
    NSMutableDictionary * groupDict = [self modelConvertDict:model];
    [groupDict setValue:communityId forKey:@"communityId"];
    [groupDict setValue:member_timestamp forKey:@"member_timestamp"];
    [groupDict setValue:customer_timestamp forKey:@"customer_timestamp"];
    [groupDict setValue:group_timestamp forKey:@"group_timestamp"];
    [groupDict setValue:room_timestamp forKey:@"room_timestamp"];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"communityInfo" withDict:groupDict];
}


/**
 *  模型转化为字典
 *
 *  @param model 组模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(CommunityInfoModel *)model
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    [dict setValue:model.pictureUrl forKey:@"pictureUrl"];
    [dict setValue:model.name forKey:@"name"];
    [dict setValue:model.address forKey:@"address"];
    [dict setValue:model.type forKey:@"type"];
    [dict setValue:model.contact forKey:@"contact"];
    [dict setValue:model.telephone forKey:@"telephone"];
    [dict setValue:model.timeStatus forKey:@"timeStatus"];
    [dict setValue:model.areaCode forKey:@"areaCode"];
    return dict;
}

/**
 *  模型转化为字典
 *
 *  @param communityId 集群ID
 *
 *  @param block       集群用于接收查询到的数组
 */
-(void)queryCommunityInfo:(NSString *)communityId communityInfoModel:(communityInfoModelBlock)communityInfoModelBlock
{
    CommunityInfoModel *groupModel=[[CommunityInfoModel alloc]init];
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"communityInfo" withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId] block:^(FMResultSet *set) {
        while ([set next]) {
            groupModel.communityId   = [set stringForColumn:@"communityId"];
            groupModel.pictureUrl    = [set stringForColumn:@"pictureUrl"];
            groupModel.name          = [set stringForColumn:@"name"];
            groupModel.address       = [set stringForColumn:@"address"];
            groupModel.type          = [set stringForColumn:@"type"];
            groupModel.contact       = [set stringForColumn:@"contact"];
            groupModel.telephone     = [set stringForColumn:@"telephone"];
            groupModel.timeStatus    = [set stringForColumn:@"timeStatus"];
        }
        if (communityInfoModelBlock) {
            communityInfoModelBlock(groupModel);
        }
    }];
}


#pragma mark -----------------删除集群基本信息
-(void)deleteCommunityInfo:(NSString *)communityId
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"communityInfo" withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId]];
}

/**
 *  通过communityId和timeKey(时间戳的字段名)来查询到时间戳
 *
 *  @param communityId   集群ID
 *
 *  @param timeKey       时间戳类型
 */
-(NSString *)getTimestamp:(NSString *)communityId timeKey:(NSString *)timeKey
{
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    NSString *str=[operation getTimeFromTable:@"communityInfo" selectTime:timeKey withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId]];
    return str;
}


/**
 *  通过communityId和timeKey(时间戳的字段名)来更新时间戳
 *
 *  @param communityId   集群ID
 *
 *  @param timeKey       时间戳类型
 *
 *  @param timeKeyValue  时间戳值
 */
-(void)updateTimestamp:(NSString *)communityId  timeKey:(NSString *)timeKey timeKeyValue:(NSString *)timeKeyValue
{
   __block CommunityInfoModel * model = nil;
    OperationDataBaseModel * operation = [[OperationDataBaseModel alloc]init];
    [self queryCommunityInfo:communityId communityInfoModel:^(CommunityInfoModel *comInfoModel)
    {
        model = comInfoModel;
     }
    ];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:timeKeyValue forKey:timeKey];
    if (model.communityId == nil)
    {
        [dic setObject:communityId forKey:@"communityId"];
        [operation insertDataTable:@"communityInfo" withDict:dic];
    }
    else
    {
        [operation updateDataFromTable:@"communityInfo" withCondition:[NSString stringWithFormat:@"where communityId = %@",communityId] withDict:dic];
    }
}
@end
